package project

import (
	"strings"

	"github.com/AlecAivazis/survey/v2"
)

func (p *Project) LoadMysqlConfig() error {
	p.MySQL = &MySQL{}

	var mySQLAddr string
	err := survey.AskOne(
		&survey.Input{
			Message: "MySQL服务地址",
			Default: "127.0.0.1:3306",
		},
		&mySQLAddr,
		survey.WithValidator(survey.Required),
	)
	if err != nil {
		return err
	}

	if strings.Contains(mySQLAddr, ":") {
		hp := strings.Split(mySQLAddr, ":")
		p.MySQL.Host = hp[0]
		p.MySQL.Port = hp[1]
	}

	survey.AskOne(
		&survey.Input{
			Message: "",
		},
		&p.MySQL.Database,
		survey.WithValidator(survey.Required),
	)

	return nil
}
